package com.eastfair.imaster.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.*;
import com.eastfair.imaster.entity.BusinessCircle;
import com.eastfair.imaster.service.BusinessCircleService;
import com.eastfair.imaster.vo.BusinessCircleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-08-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/businessCircle")
@Api(value = "BusinessCircle", tags = "")
public class BusinessCircleController extends SuperController<BusinessCircleService, Long, BusinessCircle, BusinessCirclePageQuery, BusinessCircleSaveDTO, BusinessCircleUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<BusinessCircle> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<BusinessCircle> businessCircleList = list.stream().map((map) -> {
            BusinessCircle businessCircle = BusinessCircle.builder().build();
            //TODO 请在这里完成转换
            return businessCircle;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(businessCircleList));
    }

    /**
     * 分页查询商脉圈
     * @param params 参数
     * @return 分页数据
     */
    @ApiOperation(value = "分页查询商脉圈",notes = "分页查询商脉圈")
    @PostMapping("/queryPage")
    public R<Page<BusinessCircleVO>> queryPage(@RequestBody PageParams<BusinessCircleQuery> params){

        QueryWrapper<BusinessCircle> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(BusinessCircle::getProjectId,ContextUtil.getProjectId())
                .eq(BusinessCircle::getExhibitionId,ContextUtil.getExhibitionId())
                //todo 需补充身份条件

                //todo 需补充标签筛选条件

                .eq(BusinessCircle::getIsDelete, BusinessConstant.DELETE_NO)
                .orderByDesc(BusinessCircle::getCreateTime);
        Page<BusinessCircle> page = this.baseService.page(params.buildPage(),queryWrapper);
        Page<BusinessCircleVO> voPage = ConvertUtil.convertPage(page,BusinessCircleVO.class);
        List<BusinessCircleVO> voList = voPage.getRecords();
        if (voList.isEmpty()) {
            return success(voPage);
        }
        this.baseService.assembleBusinessCircleList(voList);
        voPage.setRecords(voList);
        return success(voPage);
    }

    /**
     * 查询详情
     * @param query
     * @return
     */
    @ApiOperation(value = "查询商脉圈详情",notes = "查询商脉圈详情")
    @PostMapping("/queryDetail")
    public R<BusinessCircleVO> queryPage(@RequestBody BusinessCircleQuery query){
        QueryWrapper<BusinessCircle> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(BusinessCircle::getProjectId,ContextUtil.getProjectId())
                .eq(BusinessCircle::getId,query.getId());
        BusinessCircle businessCircle = this.baseService.getOne(queryWrapper,false);
        BusinessCircleVO businessCircleVO = BeanUtil.copyProperties(businessCircle,BusinessCircleVO.class);
        this.baseService.assembleBusinessCircle(businessCircleVO);
        return success(businessCircleVO);
    }

    /**
     * 保存
     * @param businessCircleDTO 商脉圈内容
     * @return true/false
     */
    @ApiOperation(value = "保存",notes = "保存")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody @Validated BusinessCircleDTO businessCircleDTO){
        return success(this.baseService.saveBusinessCircle(businessCircleDTO));
    }
}
